package com.party.refund.biz;


import com.party.common.constant.Constant;
import com.party.core.exception.BusinessException;
import com.party.core.model.activity.ActStatus;
import com.party.core.model.activity.Activity;
import com.party.core.model.crowdfund.Project;
import com.party.core.model.crowdfund.Support;
import com.party.core.model.crowdfund.SupportWithMember;
import com.party.core.model.crowdfund.TargetProject;
import com.party.core.model.member.MemberAct;
import com.party.core.service.activity.IActivityService;
import com.party.core.service.crowdfund.IProjectService;
import com.party.core.service.crowdfund.ISupportService;
import com.party.core.service.crowdfund.ITargetProjectService;
import com.party.core.service.member.IMemberActService;
import com.party.refund.jms.service.ISupportRefundPubulisherService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: huangzequn
 * @Date: 2019/5/16 14:54
 */
@Service
public class RefundPlanBizService {

    @Autowired
    private ITargetProjectService targetProjectService;

    @Autowired
    private IActivityService activityService;

    @Autowired
    private IMemberActService memberActService;

    @Autowired
    private IProjectService projectService;

    @Autowired
    private ISupportService supportService;

    @Autowired
    private ISupportRefundPubulisherService supportRefundPubulisherService;


    Logger logger = LoggerFactory.getLogger(getClass());

    /**
    * @Author hzq
    * @Description //众筹者下面的所有支持者退款
    * @Date 15:28 2019/5/16
    * @param project
    * @return
    **/
    public void refund(Project project) {
        //找出所有order_from表中状态为2 已支付的支持者
        List<SupportWithMember> supportList = supportService.findByProjectId(project.getId());
        if(supportList.size() > 0){
            TargetProject targetProject = targetProjectService.findByProjectId(project.getId());
            Activity activity = activityService.get(targetProject.getTargetId());
            if (Constant.IS_CROWFUND_ING.equals(project.getIsSuccess())) {
                activity.setBeCrowdfundNum(activity.getBeCrowdfundNum() - 1);
            } else if (Constant.CROWDFUND_PROJECT_SUCCESS.equals(project.getIsSuccess())) {
                activity.setCrowdfundedNum(activity.getCrowdfundedNum() - 1);
            }
            activityService.update(activity);

            // 更新报名状态
            MemberAct memberAct = memberActService.findByOrderId(targetProject.getOrderId());
            if (memberAct != null) {
                memberAct.setCheckStatus(ActStatus.ACT_STATUS_CANCEL.getCode());
                memberActService.update(memberAct);
            }
            project.setIsSuccess(Constant.CROWDFUND_PROJECT_REFUNDING);
            projectService.update(project);
        }
        for (SupportWithMember supportWithMember : supportList) {
            try {
                Support support = supportService.get(supportWithMember.getId());
                // 只有是支付成功和退款失败的才改 为退款中
                if (!Integer.valueOf(1).equals(support.getPayStatus()) && !Constant.SUPPORT_PAY_STATUS_FAIL.equals(support.getPayStatus())) {
                    continue;
                }
                support.setPayStatus(Constant.SUPPORT_PAY_STATUS_REFUNDING);
                supportService.update(support);
                supportRefundPubulisherService.send(supportWithMember.getId(),null);
            } catch (Exception e) {
                logger.error("众筹退款异常{},支持者id为{}", e, supportWithMember.getId());
                continue;
            }
        }
    }

    /**
     * 退款队列
     *
     * @param id 支持编号
     */
    public void sendRefund(String id){
        Support support = supportService.get(id);
        if (null == support){
            throw new BusinessException("支持信息不存在");
        }
        support.setPayStatus(Constant.SUPPORT_PAY_STATUS_REFUNDING);
        supportService.update(support);
        supportRefundPubulisherService.send(id,null);
    }
}
